package com.qsd.TeacherSystem.Service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qsd.TeacherSystem.Service.CourseService;
import com.qsd.TeacherSystem.dao.AnswerDao;
import com.qsd.TeacherSystem.dao.CourseDao;
import com.qsd.common.enums.ResultStatus;
import com.qsd.common.po.Answer;
import com.qsd.common.po.Course;
import com.qsd.common.util.JdbcUtil;
import com.qsd.common.vo.PageVo;

import javax.annotation.Resource;

@Service
public class CourseServiceImpl implements CourseService {
	
	@Resource
	private CourseDao courseDao;
	@Resource
	private AnswerDao answerDao;

	@Override
	@Transactional
	public Integer add(Course course, String username) {
		course.setTeacherUsername(username);
		//插入课程表
		courseDao.saveCourse(course);
		//返回结果
		return course.getId();
	}

	@Override
	public ResultStatus allow(Integer id) {
		int updateAllow = courseDao.updateAllow(id);
		if (updateAllow > 0) {
			return ResultStatus.SUCCESS;
		}
		return ResultStatus.SYSTEM_ERROR;
	}

	@Override
	@Transactional
	public ResultStatus start(Integer courseId, Integer paperId) {
		//1. 将课程状态选择为考试中
		courseDao.updateStatus(courseId, 1);
		//2. 将试卷id加入课程中
		courseDao.updatePaperId(courseId, paperId);
		return ResultStatus.SUCCESS;
	}

	@Override
	public ResultStatus stop(Integer courseId) {
		//1. 将课程状态选择为考试中
		courseDao.updateStatus(courseId, 0);
		//2. 将课程的试卷id设为空
		courseDao.updatePaperId(courseId, null);
		return ResultStatus.SUCCESS;
	}

	@Override
	public PageVo<Answer> answerList(int page, int limit, String username) {
		//answer,page,count
		List<Answer> answers = answerDao.queryByTeacherUsername(username);
		PageVo<Answer> answerPageVo = JdbcUtil.PageUtil(page, limit, answers);
		return answerPageVo;
	}

	@Override
	public Answer check(Integer id) {
		Answer answer = answerDao.queryAnswerById(id);
		return answer;
	}

	@Override
	public Course course(Integer id) {
		return null;
	}
	
}
